var app = getApp()

Page({
  data: {
    quesorder: 0,
    array: [],
    columns: [],
    survey:null,
    selectcolumn: 1,
    optioncontent: null,
    nextquesid: null,
    optionsForQuesid: [],
    questypeid: 0,
    questypearray: ['单选题', '多选题', '问答题'],
    current: "survey",
    surveyid:null,
    surveyname: null,
    surveydescription: null,
    surveyperoration: null,
    surveyendtime: "未知",
    queses: [],
    user: null,
    isimport: true,
    server: app.api.server,
  },
  onLoad: function(param) {
    app.api.api_meeting_enum({
      key: 'quescolumns'
    }).then(res => {
      this.setData({
        columns: res
      })
    })
    //todo 这里要支持传入id，进行修改。
    if (param.id){
      app.api.api_paper_showsurvey({ surveyid: param.id}).then(res=>{
        console.log(app.time.formatTime(new Date(res.data.surveyresponse.surveyendtime)))
        this.setData({
          survey: res.data.surveyresponse,
          surveyid:param.id,
          surveyendtime: app.time.formatTime(new Date(res.data.surveyresponse.surveyendtime))
        })
      })
    }
  },
  bindSwitchChange: function(e) {
    this.setData({
      isimport: e.detail.value
    })
  },
  bindDateChange: function(e) {
    
    this.setData({
      surveyendtime: e.detail.value + ' 23:00:00',
    })
  },
  surveycomplete: function(e) {
    if(this.data.surveyendtime==='未知'){
      wx.showToast({
        title: '请选择到期日期',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return;
    }
    this.setData({
      current: "ques",
      surveyname: e.detail.value.surveyname,
      surveydescription: e.detail.value.surveydescription,
      surveyperoration: e.detail.value.surveyperoration,
    })
  },
  tosurvey: function() {
    this.setData({
      current: "survey",
    })
  },
  toques: function() {
    this.setData({
      current: "ques",
    })
  },
  newques: function() {
    this.setData({
      current: "option",
    })
  },
  bindColumnChange: function(e) {
    this.setData({
      selectcolumn: parseInt(e.detail.value)
    })
  },
  bindTypeChange: function(e) {
    this.setData({
      questypeid: parseInt(e.detail.value)
    })
  },
  addoption: function() {
    var arrayy = this.data.array.concat(this.data.array.length + 1)
    this.setData({
      array: arrayy
    })
  },
  quescomplete: function(e) {
    this.setData({
      quesorder: this.data.quesorder + 1,
    })
    for (var i = 0; i < this.data.array.length; i++) {
      var optionkey = "option" + i;
      var optionnextkey = "optionnext" + i;
      var nextquesidd = e.detail.value[optionnextkey]
      if (nextquesidd == '')
        nextquesidd = this.data.quesorder + 1;

      var optionsForQuesidd = this.data.optionsForQuesid.concat([{
        optioncontent: e.detail.value[optionkey],
        nextquesid: nextquesidd
      }])
      this.setData({
        optionsForQuesid: optionsForQuesidd
      })
    }
    //填空题
    if (this.data.questypeid == 2) {
      var optionsForQuesidd = [{
        optioncontent: "",
        nextquesid: this.data.quesorder + 1
      }]
      this.setData({
        optionsForQuesid: optionsForQuesidd
      })
    }

    var quesess = this.data.queses.concat([{
      quescontent: e.detail.value.quescontent,
      questypeid: 1 + parseInt(this.data.questypeid),
      quesorder: this.data.quesorder,
      quesrequired: e.detail.value.quesrequired,
      optionsForQuesid: this.data.optionsForQuesid,
      columntype: this.data.selectcolumn
    }])
    this.setData({
      queses: quesess,
      questypeid: 0,
    })
    this.setData({
      current: "ques",
    })
    this.setData({
      array: [],
      optionsForQuesid: [],
    })
  },
  completeall: function() {

    this.setData({
      user: app.globalData.userInfo,
    })

    wx.showToast({
      title: '提交中',
      icon: 'loading',
      duration: 10000,
      mask: true
    })
    var that = this;
    wx.request({
      url: this.data.server + '/submitnewsurvey.action',
      data: {
        survey: this.data,
        queses: this.data.queses,
        user: app.globalData.userInfo,
      },
      method: 'POST',
      success: function(res) {
        wx.hideToast()
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          mask: true
        })
        if(that.data.isimport){
          wx.navigateTo({
            url: '../statistics/statistics?id=' + res.data.surveyid,
          })
        }
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  }
})